package mil.cdc.windows;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import mil.cdc.listeners.CustomKeyListener;
import mil.cdc.listeners.CustomMouseListener;
import mil.cdc.main.Main;
import mil.cdc.utils.CDCConstants;
import mil.cdc.utils.Question;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

//VS4E -- DO NOT REMOVE THIS LINE!
public class MainWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel headLbl;
	private JTextArea questionArea;
	private JScrollPane questionPane;
	private JLabel label1;
	private JTextArea choice1;
	private JScrollPane choice1Pane;
	private JLabel label2;
	private JTextArea choice2;
	private JScrollPane choice2Pane;
	private JTextArea choice3;
	private JScrollPane choice3Pane;
	private JLabel label3;
	private JLabel label4;
	private JTextArea choice4;
	private JScrollPane choice4Pane;
	private JLabel answerLbl;
	private JTextField answerField;
	private JButton submitBtn;
	private JButton prevBtn;
	private JButton nextBtn;
	private JButton closeBtn;
	private JLabel verLbl;
	private JLabel helpLbl;
	private JLabel curr;
	private JLabel of;
	private JLabel max;
	
	private ArrayList<Question> questions;
	private ArrayList<Question> wrongQuestions;
	private ArrayList<Question> rightQuestions;
	private int currentQuestionNumber = 0;
	private boolean finished = false;
	
	public MainWindow() {
		initComponents();
	}
	
	public void start() {
		finished = false;
		currentQuestionNumber = 0;
		Main.debugWindow.resetMessage();
		Main.debugWindow.addMessage("Starting Airforce CDC Practice Test");
		QuestionBankWindow qbw = new QuestionBankWindow();
		qbw.attachToMainWindow(this);
	}

	private void initComponents() {
		setTitle("Airforce CDC's Practice Test");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/mil/cdc/data/ico/af.gif")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(getHeadLbl(), new Constraints(new Leading(12, 12, 12), new Leading(6, 10, 10)));
		add(getQuestionPane(), new Constraints(new Bilateral(11, 12, 22), new Leading(27, 158, 10, 10)));
		add(getChoice1Pane(), new Constraints(new Leading(66, 825, 12, 12), new Leading(192, 46, 12, 12)));
		add(getLabel1(), new Constraints(new Leading(12, 12, 12), new Leading(200, 10, 10)));
		add(getLabel2(), new Constraints(new Leading(11, 12, 12), new Leading(256, 12, 12)));
		add(getChoice2Pane(), new Constraints(new Bilateral(66, 12, 22), new Leading(246, 45, 10, 10)));
		add(getChoice3Pane(), new Constraints(new Leading(66, 824, 12, 12), new Leading(303, 45, 12, 12)));
		add(getLabel3(), new Constraints(new Leading(11, 45, 12, 12), new Leading(309, 12, 12)));
		add(getLabel4(), new Constraints(new Leading(12, 45, 12, 12), new Leading(370, 10, 10)));
		add(getChoice4Pane(), new Constraints(new Bilateral(66, 12, 22), new Leading(359, 44, 10, 10)));
		add(getAnswerField(), new Constraints(new Leading(445, 62, 10, 10), new Leading(409, 24, 10, 10)));
		add(getAnswerLbl(), new Constraints(new Leading(386, 12, 12), new Leading(412, 12, 12)));
		add(getSubmitBtn(), new Constraints(new Leading(386, 121, 12, 12), new Leading(440, 12, 12)));
		add(getPrevBtn(), new Constraints(new Leading(386, 59, 10, 10), new Leading(472, 12, 12)));
		add(getNextBtn(), new Constraints(new Leading(451, 56, 12, 12), new Leading(472, 12, 12)));
		add(getCloseBtn(), new Constraints(new Leading(386, 121, 12, 12), new Leading(507, 10, 10)));
		add(getVerLbl(), new Constraints(new Leading(11, 12, 12), new Trailing(12, 412, 412)));
		add(getHelpLbl(), new Constraints(new Leading(386, 120, 12, 12), new Leading(539, 12, 12)));
		add(getMax(), new Constraints(new Leading(862, 10, 10), new Leading(10, 10, 10)));
		add(getOf(), new Constraints(new Leading(839, 12, 12), new Leading(10, 12, 12)));
		add(getCurr(), new Constraints(new Leading(805, 10, 10), new Leading(10, 12, 12)));
		setSize(903, 585);
	}

	private JLabel getMax() {
		if (max == null) {
			max = new JLabel();
			max.setText("max");
			max.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return max;
	}

	private JLabel getOf() {
		if (of == null) {
			of = new JLabel();
			of.setText("of");
		}
		return of;
	}

	private JLabel getCurr() {
		if (curr == null) {
			curr = new JLabel();
			curr.setText("curr");
			curr.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return curr;
	}

	private JLabel getVerLbl() {
		if(verLbl == null) {
			verLbl = new JLabel();
			verLbl.setText("v2.5.1");
		}
		return verLbl;
	}

	private JLabel getHelpLbl() {
		if (helpLbl == null) {
			helpLbl = new JLabel();
			helpLbl.setText("Press F1 For Help");
			helpLbl.setHorizontalAlignment(SwingConstants.CENTER);
			helpLbl.setAlignmentX(0.5f);
		}
		return helpLbl;
	}

	private JButton getCloseBtn() {
		if (closeBtn == null) {
			closeBtn = new JButton();
			closeBtn.setText("Close");
			closeBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CLOSE));
		}
		return closeBtn;
	}

	private JButton getNextBtn() {
		if (nextBtn == null) {
			nextBtn = new JButton();
			nextBtn.setText("-->");
			nextBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.NEXT_QUESTION));
		}
		return nextBtn;
	}

	private JButton getPrevBtn() {
		if (prevBtn == null) {
			prevBtn = new JButton();
			prevBtn.setText("<--");
			prevBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.PREV_QUESTION));
		}
		return prevBtn;
	}

	private JButton getSubmitBtn() {
		if (submitBtn == null) {
			submitBtn = new JButton();
			submitBtn.setText("Submit");
			submitBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CURRENT_QUESTION));
		}
		return submitBtn;
	}

	private JTextField getAnswerField() {
		if (answerField == null) {
			answerField = new JTextField();
			answerField.setToolTipText("Enter Answer Here");
			answerField.setAutoscrolls(false);
			answerField.addKeyListener(new CustomKeyListener(this));
		}
		return answerField;
	}

	private JLabel getAnswerLbl() {
		if (answerLbl == null) {
			answerLbl = new JLabel();
			answerLbl.setText("Answer:");
		}
		return answerLbl;
	}

	private JScrollPane getChoice4Pane() {
		if (choice4Pane == null) {
			choice4Pane = new JScrollPane();
			choice4Pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			choice4Pane.setFocusable(false);
			choice4Pane.setViewportView(getChoice4());
		}
		return choice4Pane;
	}

	private JTextArea getChoice4() {
		if (choice4 == null) {
			choice4 = new JTextArea();
			choice4.setEditable(false);
			choice4.setLineWrap(true);
			choice4.setWrapStyleWord(true);
			choice4.setFocusable(false);
		}
		return choice4;
	}

	private JLabel getLabel4() {
		if (label4 == null) {
			label4 = new JLabel();
			label4.setFont(new Font("Dialog", Font.ITALIC, 23));
			label4.setForeground(new Color(0, 64, 128));
			label4.setText("4-D");
			label4.setFocusable(false);
		}
		return label4;
	}


	private JLabel getLabel3() {
		if (label3 == null) {
			label3 = new JLabel();
			label3.setFont(new Font("Dialog", Font.ITALIC, 23));
			label3.setForeground(new Color(0, 64, 128));
			label3.setText("3-C");
			label3.setFocusable(false);
		}
		return label3;
	}

	private JScrollPane getChoice3Pane() {
		if (choice3Pane == null) {
			choice3Pane = new JScrollPane();
			choice3Pane.setViewportView(getChoice3());
		}
		return choice3Pane;
	}

	private JTextArea getChoice3() {
		if (choice3 == null) {
			choice3 = new JTextArea();
			choice3.setEditable(false);
			choice3.setLineWrap(true);
			choice3.setWrapStyleWord(true);
			choice3.setFocusable(false);
		}
		return choice3;
	}

	private JLabel getLabel2() {
		if (label2 == null) {
			label2 = new JLabel();
			label2.setFont(new Font("Dialog", Font.ITALIC, 23));
			label2.setForeground(new Color(0, 64, 128));
			label2.setText("2-B");
			label2.setFocusable(false);
		}
		return label2;
	}
	

	private JScrollPane getChoice2Pane() {
		if (choice2Pane == null) {
			choice2Pane = new JScrollPane();
			choice2Pane.setViewportView(getChoice2());
		}
		return choice2Pane;
	}

	private JTextArea getChoice2() {
		if (choice2 == null) {
			choice2 = new JTextArea();
			choice2.setEditable(false);
			choice2.setLineWrap(true);
			choice2.setWrapStyleWord(true);
			choice2.setFocusable(false);
		}
		return choice2;
	}
	
	private JScrollPane getChoice1Pane() {
		if (choice1Pane == null) {
			choice1Pane = new JScrollPane();
			choice1Pane.setViewportView(getChoice1());
		}
		return choice1Pane;
	}
	
	private JTextArea getChoice1() {
		if (choice1 == null) {
			choice1 = new JTextArea();
			choice1.setEditable(false);
			choice1.setLineWrap(true);
			choice1.setWrapStyleWord(true);
			choice1.setFocusable(true);
		}
		return choice1;
	}

	private JLabel getLabel1() {
		if (label1 == null) {
			label1 = new JLabel();
			label1.setFont(new Font("Dialog", Font.ITALIC, 23));
			label1.setForeground(new Color(0, 64, 128));
			label1.setText("1-A");
			label1.setFocusable(false);
		}
		return label1;
	}

	private JScrollPane getQuestionPane() {
		if (questionPane == null) {
			questionPane = new JScrollPane();
			questionPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			questionPane.setFocusable(false);
			questionPane.setViewportView(getQuestionArea());
		}
		return questionPane;
	}

	private JTextArea getQuestionArea() {
		if (questionArea == null) {
			questionArea = new JTextArea();
			questionArea.setLineWrap(true);
			questionArea.setWrapStyleWord(true);
			questionArea.setFocusable(false);
		}
		return questionArea;
	}

	private JLabel getHeadLbl() {
		if (headLbl == null) {
			headLbl = new JLabel();
			headLbl.setText("Question");
			headLbl.setFocusable(false);
		}
		return headLbl;
	}

	public void submitAnswer() {
		if (finished) return;
		
		try {
			int answerVal = Integer.parseInt(answerField.getText().trim());
			performCheck(answerVal);
		}
		catch (NumberFormatException ex) {
			int answerVal;
			
			String answer = answerField.getText().trim().toLowerCase();
			
			if(answer.equals("a")) {
				answerVal = 1;
			}
			else if (answer.equals("b")) {
				answerVal = 2;
			}
			else if (answer.equals("c")) {
				answerVal = 3;
			}
			else if (answer.equals("d")) {
				answerVal = 4;
			}
			else {
				answerVal = -1;
			}
			performCheck(answerVal);
		}
		
	}

	private void performCheck(int answerVal) {
		Question currentQuestion = questions.get(currentQuestionNumber);
		currentQuestion.setSelectedAnswer(answerVal);
		if (answerVal < 1 || answerVal > 4)
		{
			Main.errorWindow.displayErrorMessage("Must Enter 1-4 or A-D");
			Main.debugWindow.addMessage("Need to enter 1-4 or A-D");
			displayQuestions();
		}

		if (answerVal == currentQuestion.getAnswer())
		{
			rightQuestions.add(currentQuestion);
			Main.debugWindow.addMessage("--->Correct Answer<---");
		}
		else
		{
			wrongQuestions.add(currentQuestion);
			Main.debugWindow.addMessage("xxx>Wrong Answer<xxx");
		}

		currentQuestionNumber++;

		displayQuestions();
	}

	public void getPrevQuestion() {
		if (finished) return;
		
		currentQuestionNumber = currentQuestionNumber - 1 < 0 ? 0 : currentQuestionNumber - 1;
		
		Question prevQuestion = questions.get(currentQuestionNumber);
		if (wrongQuestions.contains(prevQuestion)) {
			wrongQuestions.remove(prevQuestion);
		}
		else if (rightQuestions.contains(prevQuestion)) {
			rightQuestions.remove(prevQuestion);
		}
		
		displayQuestions();	
	}

	public void getNextQuestion() {
		if (finished) return;
		
		wrongQuestions.add(questions.get(currentQuestionNumber));
		currentQuestionNumber++;
		displayQuestions();
	}

	public void showHelp() {
		String message = "Enter your answer between 1-4 or A,B,C,D\n" +
		"Press [Enter] or SUBMIT to Submit your Answer.\n\n" +
		"F2:\tPrevious Question\n" +
		"F3:\tNext Question\n" +
		"F5:\tHow To Create Own Tests";
		Main.errorWindow.displayErrorMessage(message, "Help");
		
	}

	public void startQuestions(ArrayList<Question> allQuestions, int numOfQuestions) {
		numOfQuestions = numOfQuestions > allQuestions.size() ? allQuestions.size() : numOfQuestions;
		questions = new ArrayList<Question>();
		wrongQuestions = new ArrayList<Question>();
		rightQuestions = new ArrayList<Question>();
		
		Collections.sort(allQuestions, new Comparator<Question>() {

			@Override
			public int compare(Question q1, Question q2) {
				return ((Integer)q1.getID()).compareTo(q2.getID());
			}
		});
		
		for (int i = 0; i < numOfQuestions; i++) {
			questions.add(allQuestions.get(i));
		}
		
		displayQuestions();
		this.setVisible(true);	
		if (Main.errorWindow.hasNoError()){
			answerField.requestFocusInWindow();
		}
	}

	private void displayQuestions() {
		if ((currentQuestionNumber + 1) > questions.size()) {
			displaySummary();
			return;
		}
		
		curr.setText(String.valueOf(currentQuestionNumber + 1));
		max.setText(String.valueOf(questions.size()));
		
		answerField.setText("");
		
		Question currentQuestion = questions.get(currentQuestionNumber);
		questionArea.setText(currentQuestion.getQuestion());
		choice1.setText(currentQuestion.getChoice(0));
		choice2.setText(currentQuestion.getChoice(1));
		choice3.setText(currentQuestion.getChoice(2));
		choice4.setText(currentQuestion.getChoice(3));
		
	}

	private void displaySummary() {
		int wrongAnswers = wrongQuestions.size();
		int rightAnswers = rightQuestions.size();
		
		float percent = Float.valueOf(String.format("%.2f", ((float)rightAnswers / (float)questions.size()) * 100f));
		
		String message = String.format("Total Questions:   %d\n\n\tCorrect Answers:" +
                            "%d\n\tWrong Answers: %d\n\n\tPercent:   %f%%",
                            wrongAnswers + rightAnswers, rightAnswers, wrongAnswers, percent);
		EndWindow ew = new EndWindow();
		ew.attachToMainWindow(this);
		ew.setMessage(message);
		ew.setVisible(true);
		ew.setPass(percent > 90);
		
		finished = true;
	}
	
	@SuppressWarnings("unchecked")
	public void viewMissedQuestions() {
		if (wrongQuestions.size() == 0) {
			Main.errorWindow.displayErrorMessage("There are no missed questions");
			return;
		}
		finished = false;
		
		questions = (ArrayList<Question>) wrongQuestions.clone();
		wrongQuestions.clear();
		rightQuestions.clear();
		
		currentQuestionNumber = 0;
		
		displayQuestions();
	}
	
	public ArrayList<Question> getReviewQuestions() {
		return wrongQuestions;
	}

	public void close() {
		Main.close();
		this.dispose();
		
	}

	public void showHowTo() {
		HowToWindow htw = new HowToWindow();
		htw.setVisible(true);
	}
	
}



